package cn.edu.neusoft.mall.controller;

import cn.edu.neusoft.framework.Exption.ServiceException;
import cn.edu.neusoft.framework.annotations.Controller;
import cn.edu.neusoft.framework.annotations.RequestMapping;
import cn.edu.neusoft.framework.annotations.RequestParam;
import cn.edu.neusoft.framework.model.ModelAndView;
import cn.edu.neusoft.mall.Utils.constant;
import cn.edu.neusoft.mall.entity.Pet;
import cn.edu.neusoft.mall.entity.User;
import cn.edu.neusoft.mall.service.impl.commentServiceimpl;
import cn.edu.neusoft.mall.service.impl.getLoveServiceimpl;
import cn.edu.neusoft.mall.service.impl.petServiceimpl;
import cn.edu.neusoft.mall.service.impl.userServiceimpl;
import cn.edu.neusoft.mall.service.impl.getLoveServiceimpl;
import cn.edu.neusoft.mall.service.petService;
import cn.edu.neusoft.mall.service.commentService;
import cn.edu.neusoft.mall.service.getLoveService;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class TestController {
    petService petService = new petServiceimpl();
    commentService commentService = new commentServiceimpl();
    getLoveService getLoveService = new getLoveServiceimpl();
    //首页
    @RequestMapping("/index")
    public ModelAndView mallIndex() {
        ModelAndView mv=new ModelAndView("index");
        mv.addObject("index_petsList",petService.getpetList());
        return  mv;
    }
    //单宠物详细页
    @RequestMapping("/dogsDetails")
    public ModelAndView dogsDetails(@RequestParam(name = "petdetail")int queryNumber) {

        ModelAndView mv=new ModelAndView("dogDetails");
        mv.addObject("dogdetails",petService.getdogsde(queryNumber));
        mv.addObject("commentList",commentService.getCommentList());
        return  mv;
    }
    @RequestMapping("/catsDetails")
    public ModelAndView catsDetails(@RequestParam(name = "petdetail")int queryNumber) {

        ModelAndView mv=new ModelAndView("catsDetails");
        mv.addObject("catdetails",petService.getcatsde(queryNumber));
        mv.addObject("commentList",commentService.getCommentList());
        return  mv;
    }
    @RequestMapping("/addComment")
    public void addComment(@RequestParam(name = "name")String name,@RequestParam(name = "comment")String comment) {
        int a = commentService.insertComment(name,comment);
        if( a!= 1){
            throw new ServiceException("添加评论失败");
        }
    }

    //搜索狗狗
    @RequestMapping("/moredogs")
    public ModelAndView moredogs(@RequestParam(name = "dogspage",defaultValue = "1")int dogspage) {

        ModelAndView mv=new ModelAndView("dogs");
        List<Pet> petList=petService.getmoredogspage(dogspage);
        mv.addObject("moredogs",petList);
        mv.addObject("countdogs",petService.getmoredogs().size());
        mv.addObject("countdogspage",dogspage);
        return  mv;
    }
    //搜索猫猫
    @RequestMapping("/morecats")
    public ModelAndView morecats(@RequestParam(name = "catsspage",defaultValue = "1")int catspage) {

        ModelAndView mv=new ModelAndView("cats");
        List<Pet> petList=petService.getmorecatspage(catspage);
        mv.addObject("morecats",petList);
        mv.addObject("countcats",petService.getmorecats().size());
        mv.addObject("countcatspage",catspage);
        return  mv;
    }
    @RequestMapping("/Havedogs")
    public ModelAndView Havedogs() {

        ModelAndView mv=new ModelAndView("Havedogs");

        return  mv;
    }
    @RequestMapping("/Havecats")
    public ModelAndView Havecats() {

        ModelAndView mv=new ModelAndView("Havecats");

        return  mv;
    }
    @RequestMapping("/addLove")
    public String addLove(@RequestParam(name = "petdetail")int queryNumber, HttpSession httpSession) {
        User user1 =(User) httpSession.getAttribute(constant.login_user);
        getLoveService.love(user1.getId(),queryNumber);
        return "redirect:myLove.action";
    }
}
